﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace jacDigitalDrum
{
    public class jacData
    {
        public string Name { get; set; }
        public string model { get; set; }
        public string a0 { get; set; }
        public string a1 { get; set; }
        public string a2 { get; set; }
        public string a3 { get; set; }
        public string a4 { get; set; }
        public string a5 { get; set; }
    }

    internal class ObjectXmlSerializer
    {
        internal static T LoadFromXml<T>(string fileName) where T : class
        {
            if (System.IO.File.Exists(fileName))
            {
                FileStream fs = null;
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    return (T)serializer.Deserialize(fs);
                }
                catch { return null; }
                finally
                {
                    if (fs != null)
                    {
                        fs.Close();
                    }
                }
            }
            else
            {
                return null;
            }
        }

        internal static void SaveToXml<T>(string fileName, T data) where T : class
        {
            SaveToXml<T>(fileName, data, true);
        }

        internal static void SaveToXml<T>(string fileName, T data, bool loggingEnabled) where T : class
        {
            FileStream fs = null;
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                serializer.Serialize(fs, data);
            }
            catch (Exception e)
            {
                if (loggingEnabled)
                    MessageBox.Show(e.Message);
                else throw;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
    }
}
